package com.back.end.Controller.App;

import com.back.end.Modal.ProductChild;
import com.back.end.Modal.ProductList;
import com.back.end.Modal.Sales;
import com.back.end.Service.AppProductService;
import com.back.end.Service.WebProductService;
import com.back.end.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@RestController
@RequestMapping("/App/product")
public class AppProductController {
    @Resource
    private AppProductService productService;

    @Resource
    private WebProductService webProductService;

    //查询商品列表
    @GetMapping("/productList")
    public Result getProductList() {
        List<ProductList> productTypes = productService.getProductList();
        return Result.success(productTypes);
    }

    //根据商品ID查询商品信息
    @GetMapping("/productById")
    public Result getProductById(int id) {
        ProductChild product = productService.getProductChildById(id);
        return Result.success(product);
    }


    @PostMapping("/addOrdersProduct")
    public Result addOrdersProduct(@RequestBody List<ProductChild> products) {
        products.forEach(item -> {
            Sales sales = new Sales();
            sales.setName(item.getName());
            sales.setProductId(item.getId());
            sales.setNumber(item.getNum());
            sales.setCreatedTime(new Timestamp(System.currentTimeMillis()));
            productService.addOrdersProduct(sales);
        });
        return Result.success("成功");
    }


    @RequestMapping("/getRankList")
    public Result getRankList(String time) {
        List<Sales> sales = webProductService.getRankList(time);
        return Result.success(sales);
    }


}


